/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import java.io.File;
import server.server.Server;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Александр Емельянов (mr.lex91@gmail.com)
 */
public class ServerStart {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String dbPath = "db\\";
        String host="localhost";
        int port=8000;
        try {
            // TODO code application logic here
            FileInputStream fis=new FileInputStream("server.ini");
            Scanner scan=new Scanner(fis);
            while (scan.hasNext())
            {
                String header=scan.next();
                header=header.substring(0, header.length()-1);
                String body=scan.nextLine();
                body=body.substring(1,body.length());
                if (header.equalsIgnoreCase("host")) host=body;
                else if (header.equalsIgnoreCase("port")) port=Integer.valueOf(body);
                else if (header.equalsIgnoreCase("database")) dbPath=body;
            }
            fis.close();
        } catch (IOException ex) {}
        try{
            Server serv=new Server(port, host, dbPath);
            serv.Start();
            Scanner scan= new Scanner(System.in);
            System.out.println("\"help\" - show help guide");
            while (true)
            {
                String str=scan.nextLine();
                Scanner sc=new Scanner(str);
                try{
                    String com=sc.next();
                    String st;
                    switch(ConsoleCodes(com))
                    {
                        case 1:
                            serv.Close();
                            return;
                        case 2:
                            st=sc.next();
                            if (st.length()==0)
                            {
                                System.out.println("Wrong command! Use \"help\"");
                                break;
                            }
                            if (new File(st).exists())
                            {
                                dbPath=st;
                                serv.changeDB(dbPath);
                            }
                            else System.out.println("Wrong dbPath");
                            break;
                        case 3:
                            serv.Start();
                            break;
                        case 4:
                            serv.Stop();
                            break;
                        case 5:
                            st=sc.next();
                            if (st.length()==0)
                            {
                                System.out.println("Wrong command! Use \"help\"");
                                break;
                            }
                            int port_new;
                            try{
                                port_new=Integer.valueOf(st);
                            }catch(NumberFormatException exc){
                                System.out.println("Wrong PORT");
                                break;
                            }
                            try{
                                serv.changePORT(port_new);
                                port=port_new;
                            }
                            catch(IOException exc){
                                System.out.println("Wrong PORT");
                            }
                            break;
                        case 6:
                            st=sc.next();
                            if (st.length()==0)
                            {
                                System.out.println("Wrong command! Use \"help\"");
                                break;
                            }
                            try{
                                serv.changeHOST(st);
                                host=st;
                            }
                            catch(IOException exc){
                                System.out.println("Wrong HOST");
                            }
                            break;
                        case 7:
                            System.out.println("\n exit - exitfrom programm"
                                    + "\n help - show this help guide"
                                    + "\n start - start server"
                                    + "\n stop - stop server"
                                    + "\n changeDB dbPath - change database path to dbPath"
                                    + "\n changeHOST HOST - change server host name to HOST"
                                    + "\n changePORT PORT - change server port to PORT"
                                    + "\n dbPath - show database path"
                                    + "\n host - server host name"
                                    + "\n port - server port\n");
                            break;
                        case 8:
                            System.out.println(port);
                            break;
                        case 9:
                            System.out.println(host);
                            break;
                        case 10:
                            System.out.println(dbPath);
                            break;
                            
                        case 0:System.out.println("Unknown command");
                    }
                } catch (NoSuchElementException ex) {
                    System.out.println("void command");
                }
            }
        } catch (IOException ex) {
            System.out.println("Wrong network settings");
        }
    }
    
    private static int ConsoleCodes(String com)
    {
        if ("exit".equals(com)) return 1;
        else if ("changeDB".equals(com)) return 2;
        else if ("start".equals(com)) return 3;
        else if ("stop".equals(com)) return 4;
        else if ("changePORT".equals(com)) return 5;
        else if ("changeHOST".equals(com)) return 6;
        else if ("help".equals(com)) return 7;
        else if ("port".equals(com)) return 8;
        else if ("host".equals(com)) return 9;
        else if ("dbPath".equals(com)) return 10;
        else return 0;
    }
}